<?php

namespace Modules\Car\Entities;

use App\Models\Car;
use App\Models\Contract\CarContract;
use App\Models\Coupon;
use App\Models\OrderService;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Modules\Manager\Entities\Department;
use App\Models\Order\OrderPrePay;
use Modules\Car\Entities\Service;
use Modules\Car\Entities\Illegal;
use App\Models\Order\RenewLease;
use App\Models\Car\ExistingEvidences;

class CarOrder extends Model
{
    use HasFactory;
    protected $guarded = [];
    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
    //outset_photo
    public function setOutsetPhotoAttribute($value)
    {
        $this->attributes['outset_photo'] = json_encode($value);
    }
    public function getOutsetPhotoAttribute($value)
    {
        return json_decode($value,true);
    }
    //return_photo
    public function setReturnPhotoAttribute($value)
    {
        $this->attributes['return_photo'] = json_encode($value);
    }
    public function getReturnPhotoAttribute($value)
    {
        return json_decode($value,true);
    }
    //送车照片
    public function setSendCarPhotoAttribute($value)
    {
        $this->attributes['send_car_photo'] = json_encode($value);
    }
    public function getSendCarPhotoAttribute($value)
    {
        return json_decode($value,true);
    }
    //送车照片
    public function setScratchImagesAttribute($value)
    {
        $this->attributes['scratch_images'] = json_encode($value);
    }
    public function getScratchImagesAttribute($value)
    {
        return json_decode($value,true);
    }
    public function expen(){
        return $this->hasMany(Expenses::class,'order_sn','order_sn');
    }
    public function user(){
        return $this->hasOne(User::class,'openid','openid');
    }
    public function car(){
        return $this->hasOne(Car::class,'id','car_series_id');
    }
    //初始车辆id
    public function initCar(){
        return $this->hasOne(Car::class,'id','init_car_series_id');
    }
    //排车
    public function gear(){
        return $this->hasOne(RecognizeDrivingLicense::class,'id','gear_car_id');
    }

    public function service(){
        return $this->hasMany(OrderService::class,'order_sn','order_sn');
    }
    public function maintenance(){
        return $this->hasMany(Service::class,'order_sn','order_sn');
    }
    public function illegals(){
        return $this->hasMany(Illegal::class,'order_sn','order_sn');
    }
    public function renewLease(){
        return $this->hasMany(RenewLease::class,'order_sn','order_sn');
    }

    public function coupon(){
        return $this->hasOne(Coupon::class,'id','coupon_id');
    }
    public function getPreServicePriceAttribute()
    {
        return $this->attributes['pre_service_price'] = $this->service_price*$this->pre_lease_days;
    }
    //pre_brokerage
    public function getPreBrokerageAttribute(){
        return $this->attributes['pre_brokerage'] = $this->brokerage*$this->pre_lease_days;
    }
    //pre_base_price
    public function getPreBasePriceAttribute(){
        return $this->attributes['pre_base_price'] = $this->base_price*$this->pre_lease_days;
    }
    //fact_service_price
    public function getFactServicePriceAttribute(){
        return $this->attributes['fact_service_price'] = $this->service_price*$this->lease_days;
    }
    //fact_brokerage
    public function getFactBrokerageAttribute(){
        return $this->attributes['fact_brokerage'] = $this->brokerage*$this->lease_days;
    }
    //fact_base_price
    public function getFactBasePriceAttribute(){
        return $this->attributes['fact_brokerage'] = $this->base_price*$this->lease_days;
    }
    public function customer(){
        return $this->hasOne(Customer::class,'id','customer_id');
    }
    public function departments(){
        return $this->hasOne(Department::class,'id','department');
    }
    public function changeCar(){
        return $this->hasOne(Car::class,'id','change_car_series_id');
    }

    public function wxCustomer(){
        return $this->hasOne(Customer::class,'phone','mini_phone');
    }
    public function phoneCustomer(){
        return $this->hasOne(Customer::class,'phone','phone');
    }

    public function orderPrePays(){
        return $this->hasOne(OrderPrePay::class,'order_sn','order_sn');
    }

    public function orderEvidence(){
        return $this->hasOne(ExistingEvidences::class,'order_sn','order_sn');
    }

    //
    public function contracts()
    {
        return $this->hasMany(CarContract::class,'order_id','id');
    }


}
